﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class PaintCommon
    {
        /// <summary>
        /// 根据矩形画圆角
        /// </summary>
        /// <param name="rectangle"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static GraphicsPath GetRoundRectangle(Rectangle rect, int r)
        {
            GraphicsPath gp = new GraphicsPath();
            int l = 2 * r;//圆弧所在矩形的边长
            //上边线段
            gp.AddLine(rect.X + r, rect.Y, rect.Right - r, rect.Y);
            //右上角1/4圆弧  270 90
            gp.AddArc(rect.Right - l, rect.Y, l, l, 270F, 90F);
            //右边竖线
            gp.AddLine(rect.Right, rect.Y + r, rect.Right, rect.Bottom - r);
            //右下角1/4圆弧  0 90
            gp.AddArc(rect.Right - l, rect.Bottom - l, l, l, 0F, 90F);
            //下边线段
            gp.AddLine(rect.Right - r, rect.Bottom, rect.X + r, rect.Bottom);
            //左下角1/4圆弧  90 90
            gp.AddArc(rect.X, rect.Bottom - l, l, l, 90F, 90F);
            //左边竖线
            gp.AddLine(rect.X, rect.Bottom - r, rect.X, rect.Y + r);
            //左上角1/4圆弧  180 90
            gp.AddArc(rect.X, rect.Y, l, l, 180F, 90F);
            return gp;
        }



        public static GraphicsPath GetRoundRectangle1(Rectangle rectangle, int ltr, int rtr, int rbr, int lbr)
        {
            int ltrd = 2 * ltr, rtrd = 2 * rtr, rbrd = 2 * rbr, lbrd = 2 * lbr;
            // 把圆角矩形分成八段直线、弧的组合，依次加到路径中 
            GraphicsPath gp = new GraphicsPath();
            gp.AddLine(new Point(rectangle.X + ltr, rectangle.Y), new Point(rectangle.Right - rtr, rectangle.Y));
            if (rtr > 0)
                gp.AddArc(new Rectangle(rectangle.Right - rtrd, rectangle.Y, rtrd, rtrd), 270F, 90F);

            gp.AddLine(new Point(rectangle.Right, rectangle.Y + rtr), new Point(rectangle.Right, rectangle.Bottom - rbr));
            if (rbr > 0)
                gp.AddArc(new Rectangle(rectangle.Right - rbrd, rectangle.Bottom - rbrd, rbrd, rbrd), 0F, 90F);


            gp.AddLine(new Point(rectangle.Right - rbr, rectangle.Bottom), new Point(rectangle.X + lbr, rectangle.Bottom));
            if (lbr > 0)
                gp.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - lbrd, lbrd, lbrd), 90F, 90F);

            gp.AddLine(new Point(rectangle.X, rectangle.Bottom - lbr), new Point(rectangle.X, rectangle.Y + ltr));
            if (ltr > 0)
                gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, ltrd, ltrd), 180F, 90F);
            return gp;
        }
    }
}
